package cn.yqx.bankshop.order.service.impl;

import cn.yqx.bankshop.order.dao.BuOrderAssessMapper;
import cn.yqx.bankshop.order.dao.BuOrderMapper;

import cn.yqx.bankshop.order.page.PageRequest;
import cn.yqx.bankshop.order.page.PageResult;
import cn.yqx.bankshop.order.page.PageUtils;
import cn.yqx.bankshop.order.pojo.BuOrder;
import cn.yqx.bankshop.order.pojo.BuOrderAssess;
import cn.yqx.bankshop.order.pojo.BuOrderExample;
import cn.yqx.bankshop.order.service.OrderService;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class OrderServiceImpl implements OrderService {

    private static final String ADD_ASSESS_KEY = "ADD_ASSESS" ;
    private static final String ALL_ASSESS_KEY = "ALL_ASSESS" ;
    private static final String GOOD_ASSESS_KEY = "GOOD_ASSESS" ;
    private static final String BAD_ASSESS_KEY = "BAD_ASSESS" ;
    private static final String HAS_PICTURE_KEY = "HAS_PICTURE" ;

    private static final String ADD_ASSESS_COUNT_KEY = "addAssessCount" ;
    private static final String ALL_ASSESS_COUNT_KEY = "allAssessCount" ;
    private static final String GOOD_ASSESS_COUNT_KEY = "goodAssessCount" ;
    private static final String BAD_ASSESS_COUNT_KEY = "badAssessCount" ;
    private static final String HAS_PICTURE_COUNT_KEY = "hasPictureCount" ;

    private static final Integer ASSESS_COUNT_MAP_COUNT = 5 ;

    @Autowired
    BuOrderMapper mapper ;
    @Autowired
    BuOrderAssessMapper buOrderAssessMapper ;
    @Override
    public List<Map<String, Object>> findSuppliersBySales() {
        return mapper.findSuppliersBySales();
    }

    @Override
    public List<Map<String, Object>> findGoodsTop5(Long supplierId) {
        return mapper.findGoodsTop5(supplierId) ;
    }

    @Override
    public List<Map<String, Object>> findEvaluateBySupplier(Long supplierId) {
        return buOrderAssessMapper.findEvaluateBySupplier(supplierId) ;
    }

    @Override
    public List<Map<String, Object>> findGoodsBySales() {
        return mapper.findGoodsBySales();
    }

    @Override
    public PageResult getOrderAssessList(PageRequest pageRequest, String assessType, Long goodsId , String assessTime) {
        return  PageUtils.getPageResult(pageRequest, getPageInfo(pageRequest,assessType,goodsId,assessTime)) ;
    }

    @Override
    public Map<String, Integer> getAssessCountByGoodId(Long goodsId, String assessTime) {

        Map<String,Integer> assessCountMap = new HashMap<String,Integer>(ASSESS_COUNT_MAP_COUNT) ;

        assessCountMap.put(ADD_ASSESS_COUNT_KEY,buOrderAssessMapper.getAssessCountByGoodId(ADD_ASSESS_KEY,goodsId,assessTime)) ;
        assessCountMap.put(ALL_ASSESS_COUNT_KEY,buOrderAssessMapper.getAssessCountByGoodId(ALL_ASSESS_KEY,goodsId,assessTime)) ;
        assessCountMap.put(GOOD_ASSESS_COUNT_KEY,buOrderAssessMapper.getAssessCountByGoodId(GOOD_ASSESS_KEY,goodsId,assessTime)) ;
        assessCountMap.put(BAD_ASSESS_COUNT_KEY,buOrderAssessMapper.getAssessCountByGoodId(BAD_ASSESS_KEY,goodsId,assessTime)) ;
        assessCountMap.put(HAS_PICTURE_COUNT_KEY,buOrderAssessMapper.getAssessCountByGoodId(HAS_PICTURE_KEY,goodsId,assessTime)) ;

        return assessCountMap;
    }

    @Override
    public PageResult  findOrdersGROUPByOrderNo(PageRequest pageRequest ,Map<String ,Object> parameterMap) {
        return PageUtils.getPageResult(pageRequest, getPageInfoBuOrder(pageRequest,parameterMap)) ;
    }

    @Override
    public List<BuOrder> findAllOrders() {
        BuOrderExample example = new BuOrderExample() ;
        return mapper.selectByExample(example);
    }

    @Override
    public int updateGoodPicture(Long goodsId, String picture) {
        return mapper.updateGoodPicture(goodsId,picture);
    }

    @Override
    public Map<String, Integer> findOrdersByStatus(Map<String,Object> map) {
        return mapper.findOrdersByStatus(map);
    }

    @Override
    public List<BuOrder> findOrderByNo(String orderId) {
        BuOrderExample example = new BuOrderExample() ;
        example.or().andOrderNoEqualTo(orderId);
        return mapper.selectByExample(example);
    }

    @Override
    public List<BuOrder> findRecentOrder(Long userId) {
        BuOrder order = mapper.findRecentOrder(userId) ;
        List<BuOrder> orders = mapper.findOrdersByOrderNo(order.getOrderNo());
        return orders;
    }

    @Override
    public PageResult reconciliation(PageRequest pageRequest, JSONObject parameter) {
        return PageUtils.getPageResult(pageRequest, getPageInfoReconciliation(pageRequest,parameter)) ;
    }

    @Override
    public List<BuOrder> findOrdersByaccountNo(Long userId, String accountNo) {
        return mapper.findOrdersByaccountNo(userId,accountNo);
    }

    @Override
    public HashMap<String, Integer> findReconciliationByStatus(Map map) {
        return mapper.findReconciliationByStatus(map);
    }

    @Override
    public Map<String, Object> consumptionByUserid(Long userId) {
        return mapper.consumptionByUserid(userId) ;
    }

    /**
     * 调用分页插件完成分页
     * @return
     */
    private PageInfo<BuOrderAssess> getPageInfo(PageRequest pageRequest,String assessType,Long goodsId ,String assessTime) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<BuOrderAssess> buOrderAssesses = buOrderAssessMapper.selectPage(assessType,goodsId,assessTime);
        return new PageInfo<BuOrderAssess>(buOrderAssesses);
    }

    private PageInfo<BuOrder> getPageInfoBuOrder(PageRequest pageRequest,Map<String ,Object> parameterMap){
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<BuOrder> buOrders = mapper.findOrdersGROUPByOrderNo(parameterMap) ;
        if(!buOrders.isEmpty()){
            buOrders.forEach((item)->{
                List<BuOrder> orders = mapper.findOrdersByOrderNo(item.getOrderNo()) ;
                item.setOrderList(orders);
            });
        }
        return new PageInfo<BuOrder>(buOrders) ;
    }
    private PageInfo<Map<String,Object>> getPageInfoReconciliation(PageRequest pageRequest,Map<String ,Object> parameterMap){
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String,Object>> list = mapper.reconciliation(parameterMap) ;

        return new PageInfo<Map<String ,Object>>(list) ;
    }
}
